/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "NodeCpp/Crypto/SRP6.h"
#include <cassert>
#include <cstring>
#include <NodeCpp/Crypto/Sha1.h>
using NodeCpp::Crypto::Sha1Hash;

namespace NodeCpp {
namespace Crypto {

    const char* SRP6::N = "894B645E89E1535BBDAD5B8B290650530801B18EBFBF5E8FAB3C82872A3E9BB7";
    const std::uint32_t SRP6::g = 7;
    const std::uint8_t SRP6::k = 3;

    void SRP6::M(Crypto::BigNumber& _S, std::uint8_t* _Out, std::size_t _OutSize)
    {
        NODECPP_ASSERT(_OutSize == 40 && _Out != NULL);

        std::uint8_t _T[32];
        std::uint8_t _T1[16];
        memcpy(_T, _S.AsByteArray(32), 32);

        for (int _I = 0; _I < 16; ++_I) {
            _T1[_I] = _T[_I * 2];
        }
        Sha1Hash _Sha;
        _Sha.Initialize();
        _Sha.UpdateData(_T1, 16);
        _Sha.Finalize();
        for (int _I = 0; _I < 20; ++_I) {
            _Out[_I * 2] = _Sha.digest()[_I];
        }

        for (int _I = 0; _I < 16; ++_I) {
            _T1[_I] = _T[_I * 2 + 1];
        }
        _Sha.Initialize();
        _Sha.UpdateData(_T1, 16);
        _Sha.Finalize();
        for (int _I = 0; _I < 20; ++_I) {
            _Out[_I * 2 + 1] = _Sha.digest()[_I];
        }
    }

}}
